package com.example.apiclientsdk.core;

import com.example.apiclientsdk.util.StringUtil;

import java.util.HashMap;
import java.util.Map;

public abstract class AbstractApiClient {

    protected String accessKey;

    protected String secretKey;

    public AbstractApiClient(String accessKey, String secretKey) {
        this.accessKey = accessKey;
        this.secretKey = secretKey;
    }

    /**
     * 构建请求头
     */
    protected Map<String, String> buildHeaders() {
        Map<String, String> headers = new HashMap<>();

        //1.ak和sk密钥
        headers.put("accessKey", accessKey);
        //2.时间戳字段
        String timeStamp = StringUtil.generateTimeStamp();
        headers.put("timestamp", timeStamp);
        //3.随机数字段
        String nonceStr = StringUtil.generateRandom();
        headers.put("nonce", nonceStr);
        //4.加密字段
        String sign = StringUtil.generateSign(secretKey, timeStamp, nonceStr);
        headers.put("sign", sign);

        return headers;
    }

}
